//
//  DJHomeViewController.m
//  WeiboDemo
//
//  Created by Bworm on 15/1/29.
//  Copyright (c) 2015年 Bworm. All rights reserved.
//

#import "DJHomeViewController.h"
#import "DJTitleButton.h"
#import "DJPopMenuView.h"
#import "DJScanViewController.h"

@interface DJHomeViewController ()
//@property (nonatomic, strong) UIButton *titleButton;
@end

@implementation DJHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 可以将方法写入分类,这样其他控制器均可以调用,并且写成类方法
    /*
    self.navigationItem.leftBarButtonItem = [self itemWithImage:[UIImage imageNamed:@"navigationbar_friendsearch"]
                                                      highImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"]];

    self.navigationItem.rightBarButtonItem = [self itemWithImage:[UIImage imageNamed:@"navigationbar_pop"]
                                                       highImage:[UIImage imageNamed:@"navigationbar_pop_highlighted"]];
     */
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"navigationbar_friendsearch"]
                                                                 highImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"]
                                                                    target:self
                                                                    action:@selector(friendSearch)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"navigationbar_pop"]
                                                                  highImage:[UIImage imageNamed:@"navigationbar_pop_highlighted"]
                                                                     target:self
                                                                     action:@selector(scan)];
    
    DJTitleButton *titleButton = [[DJTitleButton alloc] init];
    [titleButton setTitle:@"首页" forState:UIControlStateNormal];
    [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
    [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];
    
    [titleButton sizeToFit];
    
    [titleButton addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
//    self.titleButton = titleButton;
    
    self.navigationItem.titleView = titleButton;
}

- (void)friendSearch
{

}

- (void)scan
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"DJScanVIewController" bundle:nil];
    DJScanViewController *scanVc = [sb instantiateInitialViewController];
    [self presentViewController:scanVc animated:YES completion:nil];
}

#pragma mark - 按钮的内部方法
UIWindow *_window;
- (void)titleBtnClick:(DJTitleButton *)titleBtn
{
    // 切换按钮的显示
    titleBtn.selected = !titleBtn.selected;// 1赋值0 0赋值1 向下=向上 向上=向下
    
    UITableView *pop = [[UITableView alloc] init];
    pop.backgroundColor = IWColor(76, 76, 76, 0);
    pop.size = CGSizeMake(150, 200);
    
    [DJPopMenuView popFrom:titleBtn content:pop dismiss:^{
        titleBtn.selected = !titleBtn.selected;
    }];
}

//- (void)coverClick:(UIButton *)btn
//{
//    _window = nil;
//    _titleButton.selected = !_titleButton.selected;
//}

//- (void)popFrom:(UIView *)fromView content:(UIView *)content
//{
//    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    _window.hidden = NO;
//    _window.windowLevel = UIWindowLevelAlert;
//    
//    UIButton *cover = [[UIButton alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    [cover setBackgroundColor:[UIColor clearColor]];
//    [self.navigationController.view addSubview:cover];
//    
//    [cover addTarget:self action:@selector(coverClick:) forControlEvents:UIControlEventTouchUpInside];
//    [_window addSubview:cover];
//    
//    // 创建容器来显示content
//    UIImageView *menuView = [[UIImageView alloc] init];
//    menuView.userInteractionEnabled = YES;
//    UIImage *image = [UIImage imageNamed:@"popover_background"];
//    menuView.image = image;
//    [cover addSubview:menuView];
//
//    // 设置tableView在menuVc中显示的范围,设置间距
//    content.x = 15;
//    content.y = 18;
//    [menuView addSubview:content];
//    
//    CGFloat menuViewW = CGRectGetMaxX(content.frame) + content.x;
//    CGFloat menuViewH = CGRectGetMaxY(content.frame) + content.y;
//    menuView.frame = CGRectMake(0, 0, menuViewW, menuViewH);
//    
//    CGRect resultFrame = [_window convertRect:fromView.frame fromView:fromView.superview];
//    
//    menuView.y = CGRectGetMaxY(resultFrame) + 3;
//    
//    CGPoint resultCenter = [_window convertPoint:fromView.center fromView:fromView.superview];
//    
//    menuView.centerX = resultCenter.x;
//    
//    _window.hidden = NO;
//}
//
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}


@end
